import SystemMenu from '../models/SystemMenu.js'
import { handleTreeList, isUrl } from '../utils/helper.js';

class MenuService
{
    static async getUserMenuList(adminId)
    {
        const menuList = await SystemMenu.getList({ where: {}, raw: true }, ['id', 'system_permission_id', 'parent_id', 'name', 'icon', 'path', 'sort', 'status', 'view'], 0, 0, [], [], false)

        menuList.list.forEach((v) => {
            // 处理 path：如果不为空且不是 URL，补充 /
            if (v.path && !isUrl(v.path)) {
              v.path = '/' + v.path.replace(/^\/+/, ''); // 去掉多余的斜杠并加上 /
            }
          
            // 设置 meta
            v.meta = { title: v.name, icon: v.icon }
          
            // 根据 status 设置 hidden
            v.hidden = v.status === 1 ? 0 : 1
          
            // 删除 icon
            delete v.icon
        })
        
        let tree = handleTreeList(menuList.list)

        tree = tree.filter(v => v.children && v.children.length > 0)

        const menu = [
            {
                "parent_id": 0,
                "path": "/",
                "name": "Home",
                "component": "Layout",
                "redirect": "/dashboard",
                "status": 1,
                "children":
                    [
                        {
                            "path": "dashboard",
                            "status": 1,
                            "meta":
                                {
                                    "title": "首页",
                                    "icon": "dashboard",
                                },
                            "name": "首页",
                            "component": "views/dashboard/index"
                        }
                    ],
                // "alwaysShow"=>true
            },
        ]

        tree.unshift(...menu)

        return tree
    }
}

export default MenuService
